<?php
/**
 * User: Hufeng
 * Date: 2015/12/15 17:36
 * Desc: 核心文件
 */
namespace SasPHP;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

class SasPHP{

    static public function start() {
        // 注册AUTOLOAD方法
        spl_autoload_register('SasPHP\SasPHP::autoload');
        //下一行本地环境注释掉下
//        register_shutdown_function('SasPHP\SasPHP::handleFatal');
        require_once BASEDIR.'/App/Common/Common/function.php';
        return self::dispatch();
    }

    // 异常处理
    static public function handleFatal(){
        $errorMsg = error_get_last();
        $log = new Logger('yltsas');
        $date = date('Ymd');
        if($_SERVER['request_uri'] != '/favicon.ico' && stripos($errorMsg['message'],'swoole_http_response') === false){
            $log->pushHandler(new StreamHandler('/var/www/staticRes/Runtime/'.$date.'.log', Logger::WARNING));
            $log->error($errorMsg['message']."====>\n".$errorMsg['file']."=====>\n".$errorMsg['line']);
        }
        $_ENV->end();
    }

    static public function autoload($class) {
        require_once BASEDIR.'/'.str_replace('\\', '/', $class).'.php';
    }

    static public function dispatch(){
        /*正式环境*/
//        $uri = $_SERVER['request_uri'];
        /*本地环境*/
        $uri = $_SERVER['REQUEST_URI'];

        $index = stripos($uri,'?');
        if($index){
            $uri = substr($uri,0,$index);
        }
        list($c, $v) = explode('/', trim($uri, '/'));
        $c = ucwords($c);
        return self::exec($c,$v);
    }

    static public function exec($c,$v){
        if(!$c){
            return false;
        }
        $class = '\\App\\Controller\\'.$c;
        $obj = new $class();
        return $obj->$v();
    }
}

